package com.banciyuan.bcywebview.biz.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.myinterface.PushActionInterface;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.apprecommend.RecommendAcgActivity;
import com.banciyuan.bcywebview.biz.pwd.FindPwdActivity;
import com.banciyuan.bcywebview.biz.qqapi.QQEntryActivity;
import com.banciyuan.bcywebview.biz.register.PhoneRegisterActivity;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.http.NetUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.sns.weibo.WeiboLoginHelper;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.tencent.tauth.Tencent;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEmailEt;
    private EditText mPwdEt;
    private FrameLayout mLoginPgb;

    private LoginHelper mHelper;
    private PushActionInterface mLoginInterface;
    private TextView tvLogin;
    private TextView pwdForget;
    public WeiboLoginHelper mWeiBoHelper;
    private SsoHandler mSsoHandler;

    private int QQLOGIN = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);
        initArgs();
        initUi();
        initAction();
    }

    @Override
    protected void onPause() {
        super.onPause();
        SPHelper.putString(this, SPConstant.LOGINEMAIL, mEmailEt.getText().toString());
    }

    @Override
    protected void initArgs() {
        mLoginInterface = new PushActionInterface() {
            @Override
            public void onNetError(String result) {
                onNetworkError(result);
            }

            @Override
            public void PushSuccess(String result) {
            }
        };
        mHelper = new LoginHelper(this, mLoginInterface);
        mSsoHandler = new SsoHandler(this);
        mWeiBoHelper = new WeiboLoginHelper(this, mSsoHandler);
    }

    @Override
    protected void initUi() {
        mEmailEt = (EditText) findViewById(R.id.login_email_edt);
        mPwdEt = (EditText) findViewById(R.id.login_pass_edt);
        mLoginPgb = (FrameLayout) findViewById(R.id.login_login_progressbar);
        tvLogin = (TextView) findViewById(R.id.tv_login);
        pwdForget = (TextView) findViewById(R.id.forget_password);
        mEmailEt.setText(SPHelper.getString(this, SPConstant.LOGINEMAIL, ""));
    }

    @Override
    protected void initAction() {
        tvLogin.setOnClickListener(this);
        pwdForget.setOnClickListener(this);
        findViewById(R.id.iv_cancle).setOnClickListener(this);
        findViewById(R.id.ll_register).setOnClickListener(this);
        findViewById(R.id.rl_weibo_login).setOnClickListener(this);
        findViewById(R.id.rl_qq_login).setOnClickListener(this);
        findViewById(R.id.not_login_show).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login:
                if (!NetUtils.checkNetwork(this)) onNetworkError("");
                else doLogin();
                break;
            case R.id.iv_cancle:
                finish();
                overridePendingTransition(R.anim.umeng_socialize_fade_in, R.anim.umeng_socialize_fade_out);
                break;
            case R.id.ll_register:
                gotoUtil.replaceActAmin(this, PhoneRegisterActivity.class, R.anim.base_slide_right_in, R.anim.base_slide_left_out);
                break;
            case R.id.not_login_show:
                finish();
                overridePendingTransition(R.anim.umeng_socialize_fade_in, R.anim.umeng_socialize_fade_out);
                break;
            case R.id.rl_qq_login:
                doQQLogin();
                break;
            case R.id.rl_weibo_login:
                doWeiboLogin();
                break;
            case R.id.forget_password:
                gotoUtil.gotoAct(this, FindPwdActivity.class);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.umeng_socialize_fade_in, R.anim.umeng_socialize_fade_out);
    }

    public void doWeiboLogin() {
        mWeiBoHelper.doWeiboLogin();
    }

    public void doQQLogin() {
        Tencent.createInstance(MyConstants.QQ_LOGIN_APP_ID, getApplicationContext()).logout(
                getApplicationContext());
        startActivityForResult(new Intent(this, QQEntryActivity.class), QQLOGIN);
    }

    @Override
    protected void onNetworkError(String result) {
        mLoginPgb.setVisibility(View.GONE);
        MyToast.show(this, result);
    }

    private void doLogin() {
        mLoginPgb.setVisibility(View.VISIBLE);
        mHelper.doLogin(mEmailEt.getText().toString(), mPwdEt.getText().toString());
    }

    private boolean isShowRecommend() {
        return SPHelper.getBoolean(this, SPConstant.BCY_SHOWRECOMMEND, false);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == QQLOGIN && resultCode == RESULT_OK) {
            if (isShowRecommend()) {
                finish();
                gotoUtil.replaceMainClearTop(this);
            } else {
                gotoUtil.replaceAct(this, RecommendAcgActivity.class);
            }
        } else {
            if (mSsoHandler != null) {
                mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
            }
        }
    }
}